﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class CartDAO:IData
 	{
		public CartDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(ref Cart item, int t) {
            SqlParameter p = new SqlParameter();
            if( t == 0)
                p.Direction = ParameterDirection.Output;
            p.ParameterName = "@" + Cart.c_id;
            p.Value = item.id;
            Object obj = item.id;
            int i =
            connec.ExecuteNonQuery("sp_Cart", CommandType.StoredProcedure,p,ref obj,
                 new SqlParameter("@flag", t), new SqlParameter("@" + Cart.c_customersId, item.customersId),
                new SqlParameter("@" + Cart.c_address, item.address), new SqlParameter("@" + Cart.c_create_on, item.create_on),
                new SqlParameter("@" + Cart.c_total, item.total), new SqlParameter("@" + Cart.c_discount, item.discount),
                new SqlParameter("@" + Cart.c_payment, item.payment), new SqlParameter("@" + Cart.c_arrears, item.arrears),
                new SqlParameter("@" + Cart.c_credit, item.credit), new SqlParameter("@" + Cart.c_status, item.status),
                new SqlParameter("@" + Cart.c_orderId, item.orderId), new SqlParameter("@" + Cart.c_isActive, item.isActive),
                new SqlParameter("@" + Cart.c_fullName, item.fullName), new SqlParameter("@" + Cart.c_phone, item.phone));
            if(t == 0)
                item.id = (long)obj;

            return i;
		}

		public  Cart GetItem(IDataReader dr)
		{
            Cart obj = new Cart();
            obj.id = Convert.ToInt64(dr[Cart.c_id]);
            obj.customersId = Convert.ToInt64(dr[Cart.c_customersId]);
            obj.address = Convert.ToString(dr[Cart.c_address]);
            obj.create_on = Convert.ToDateTime(dr[Cart.c_create_on]);
            obj.total = Convert.ToDecimal(dr[Cart.c_total]);
            obj.discount = Convert.ToDecimal(dr[Cart.c_discount]);
            obj.payment = Convert.ToDecimal(dr[Cart.c_payment]);
            obj.arrears = Convert.ToDecimal(dr[Cart.c_arrears]);
            obj.credit = Convert.ToString(dr[Cart.c_credit]);
            obj.status = Convert.ToInt32(dr[Cart.c_status]);
            obj.orderId = Convert.ToInt32(dr[Cart.c_orderId]);
            obj.isActive = Convert.ToInt32(dr[Cart.c_isActive]);
            obj.fullName = Convert.ToString(dr[Cart.c_fullName]);
            obj.phone = Convert.ToString(dr[Cart.c_phone]);
            return obj;
		}

		public  List<Cart> GetAll()
		{
			return GetAll("select * from " + Cart.b_Cart, CommandType.Text);
		}

        private List<Cart> GetAll(String strSQL, CommandType commandtype)
        {
            List<Cart> list = new List<Cart>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }

        /// <summary>
        /// Lay dang sach gio hang 
        /// </summary>
        /// <param name="Status">0 chua giao,1 dang giao,2 da giao</param>
        /// <returns></returns>
        public List<Cart> getCartsByStatus(int Status)
        {
            List<Cart> list = new List<Cart>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_getCartsByStatus";
            command.Connection = connection;
            command.Parameters.Add("@status", SqlDbType.Int).Value = Status;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }

        /// <summary>
        /// Lay gio hanh theo usersId
        /// </summary>
        /// <param name="Status"></param>
        /// <returns></returns>
        public List<Cart> getCartByUsers(long customersId)
        {
            List<Cart> list = new List<Cart>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_CartByUsers";
            command.Connection = connection;
            command.Parameters.Add("@customersId", SqlDbType.Int).Value = customersId;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
		public  Cart GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}

	}

 }
